package com.slf.engine.threads;

import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

import com.slf.engine.base.IBaseDao;
import com.slf.engine.bo.Order;
import com.slf.engine.common.SysContext;
import com.slf.engine.services.IBusService;
import com.slf.engine.services.helper.ServiceHelper;
import com.slf.engine.services.impl.BusServiceImpl;
import com.slf.engine.utils.DateUtils;
import com.slf.engine.utils.Factory;

public class SendThread extends Thread {

	private static final Logger log = Logger.getLogger(SendThread.class);
	
	public SendThread(ThreadGroup group)
	{
		super(group, "SEND_THREAD" + Thread.currentThread().getId());
		log.log(Priority.INFO, "SEND_THREAD RUN SUCCESS");
	}
	
	public void run()
	{
		IBusService busService = new BusServiceImpl();
		IBaseDao baseDao = (IBaseDao) Factory.getInstantBiz("baseDao");
		while(SysContext.IS_TREAD_RUN)
		{
			if(SysContext.IS_START)
			{
				Order order = busService.getOneOrder();
				if(order != null)
				{
					try {
						order.setCzrq(DateUtils.strDate("yyyyMMdd"));
						order.setCzsj(DateUtils.strDate("HHmmss"));
						baseDao.modify(order, "hasSendOrder");
						order = busService.doService(order);
						if(order != null)
						{
							busService.doBusResult(order);
						}
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						log.log(Priority.ERROR, "数据库连接出现问题");
					}
				}else{
					try {
						log.log(Priority.INFO, "当前没有交易订单，3秒钟后重新获取");
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
					}
				}
			}else{
				try {
					log.log(Priority.INFO, "SEND_THREAD等待上游服务连接，3秒钟后重新获取");
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
				}
			}
			
		}
	}
}
